package com.shushan.manager;

import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TimePicker;

import com.shushan.base.NavigationLoseActivity;
import com.shushan.db.RelaxTimeDao;
import com.shushan.manager.bean.RelaxTimeBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.StringTools;
import com.shushan.util.ToolUtils;
import com.shushan.util.UmengEventUtil;

import org.w3c.dom.Text;

import java.util.Calendar;
import java.util.List;

/**
 * @ClassName: UseTimeAddActivity
 * @Desciption: 新增手机使用时间段
 * @author: yichaohua
 * @date: 2018-07-18
 */
public class UseTimeAddActivity extends NavigationLoseActivity implements RadioGroup.OnCheckedChangeListener,View.OnClickListener {

    private static final String TAG = "UseTimeAddActivity";

    private RadioGroup radioGroup;
    private CheckBox checkBox0,checkBox1,checkBox2,checkBox3,
            checkBox4,checkBox5,checkBox6;
    private LinearLayout startTimeLayout;
    private LinearLayout endTimelayout;
    private TextView startTextView;
    private TextView endTextView;
    private Button addButton;
    private EditText planNmaeEdit;

    private int startHour = 0;
    private int startMin = 0;
    private int endHour = 0;
    private int endMin = 0;

    private RelaxTimeDao relaxTimeDao;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle("新增禁止使用时间段");

        setContentView(R.layout.use_time_add_activity);

        relaxTimeDao = new RelaxTimeDao(this);

        initView();

    }

    private void initView() {
        startTextView = findViewById(R.id.use_time_start_time_tv);
        endTextView = findViewById(R.id.use_time_start_end_tv);
        startTimeLayout = findViewById(R.id.use_time_start_layout);
        startTimeLayout.setOnClickListener(this);
        endTimelayout = findViewById(R.id.use_time_end_layout);
        endTimelayout.setOnClickListener(this);
        checkBox0 = findViewById(R.id.use_time_check_0);
        checkBox0.setOnClickListener(this);
        checkBox1 = findViewById(R.id.use_time_check_1);
        checkBox1.setOnClickListener(this);
        checkBox2 = findViewById(R.id.use_time_check_2);
        checkBox2.setOnClickListener(this);
        checkBox3 = findViewById(R.id.use_time_check_3);
        checkBox3.setOnClickListener(this);
        checkBox4 = findViewById(R.id.use_time_check_4);
        checkBox4.setOnClickListener(this);
        checkBox5 = findViewById(R.id.use_time_check_5);
        checkBox5.setOnClickListener(this);
        checkBox6 = findViewById(R.id.use_time_check_6);
        checkBox6.setOnClickListener(this);
        radioGroup = findViewById(R.id.use_time_tag_rg);
        radioGroup.setOnCheckedChangeListener(this);
        addButton = findViewById(R.id.use_time_add_btn);
        addButton.setOnClickListener(this);
        planNmaeEdit = findViewById(R.id.use_time_plan_edit);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.use_time_schoolday_rb:
                checkBox0.setChecked(false);
                checkBox1.setChecked(true);
                checkBox2.setChecked(true);
                checkBox3.setChecked(true);
                checkBox4.setChecked(true);
                checkBox5.setChecked(true);
                checkBox6.setChecked(false);
                break;
            case R.id.use_time_weekend_rb:
                checkBox0.setChecked(true);
                checkBox1.setChecked(false);
                checkBox2.setChecked(false);
                checkBox3.setChecked(false);
                checkBox4.setChecked(false);
                checkBox5.setChecked(false);
                checkBox6.setChecked(true);
                break;
            case R.id.use_time_daily_rb:
                checkBox0.setChecked(true);
                checkBox1.setChecked(true);
                checkBox2.setChecked(true);
                checkBox3.setChecked(true);
                checkBox4.setChecked(true);
                checkBox5.setChecked(true);
                checkBox6.setChecked(true);
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.use_time_check_0:
            case R.id.use_time_check_1:
            case R.id.use_time_check_2:
            case R.id.use_time_check_3:
            case R.id.use_time_check_4:
            case R.id.use_time_check_5:
            case R.id.use_time_check_6:
                radioGroup.clearCheck();
                break;
            case R.id.use_time_start_layout:
                showTimePickerDialog(true);
                break;
            case R.id.use_time_end_layout:
                showTimePickerDialog(false);
                break;
            case R.id.use_time_add_btn:
                if(valide()){
                    addTime();
                    UmengEventUtil.onEvent(BabyApplication.getInstance(),"time_manager_click_id","add");
                }
                break;
            default:
                break;
        }
    }

    private void showTimePickerDialog(final boolean isStartTime) {
        Calendar calendar = Calendar.getInstance();
        new TimePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        if(isStartTime){
                            startHour = hourOfDay;
                            startMin = minute;

                            startTextView.setText(StringTools.formatTime(hourOfDay,minute));
                        }else {
                            endHour = hourOfDay;
                            endMin = minute;
                            endTextView.setText(StringTools.formatTime(hourOfDay,minute));
                        }
                    }
                }
                , calendar.get(Calendar.HOUR_OF_DAY)
                , calendar.get(Calendar.MINUTE)
                ,true)
                .show();
    }


    private boolean valide() {
        String planName = planNmaeEdit.getText().toString();
        if(TextUtils.isEmpty(planName)){
            showToast("请输入计划名称");
            return false;
        }

        if(getFrequDay() <= 0){
            showToast("请选择执行的星期");
            return false;
        }

        if((endHour == 0) && (endMin == 0)){
            showToast("请设置结束时间");
            return false;
        }

        if(endHour < startHour){
            showToast("开始时间不能大于结束时间");
            return false;
        }

        if((endHour == startHour) && (endMin <= startMin)){
            showToast("结束时间必须大于开始时间");
            return false;
        }

        List<RelaxTimeBean> relaxTimeBeanList = relaxTimeDao.queryForAll();
        if(relaxTimeBeanList.size() > 0){
            for (RelaxTimeBean relaxTimeBean : relaxTimeBeanList){
                int start = relaxTimeBean.startHour * 60 + relaxTimeBean.startMin;
                int end = relaxTimeBean.endHour * 60 + relaxTimeBean.endMin;
                int addStart = startHour * 60 + startMin;
                int addEnd = endHour * 60 + endMin;

                if((addStart >= start && addStart < end) || (addEnd > start && addEnd <= end) ){
                    showToast("添加的时间段不能与其他重复");
                    return false;
                }
            }
        }

        return true;
    }

    private void addTime() {
        String planName = planNmaeEdit.getText().toString();

        RelaxTimeBean relaxTimeBean = new RelaxTimeBean(planName,getFrequDay(),
                startHour,startMin,endHour,endMin);

        relaxTimeDao.add(relaxTimeBean);

        onBackAction(100);
    }

    private int getFrequDay(){
        int frequ = 0;

        if(checkBox0.isChecked()){
            frequ |= 1;
        }

        if(checkBox1.isChecked()){
            frequ |= (1<<1);
        }

        if(checkBox2.isChecked()){
            frequ |= (1<<2);
        }

        if(checkBox3.isChecked()){
            frequ |= (1<<3);
        }

        if(checkBox4.isChecked()){
            frequ |= (1<<4);
        }

        if(checkBox5.isChecked()){
            frequ |= (1<<5);
        }

        if(checkBox6.isChecked()){
            frequ |= (1<<6);
        }

        return frequ;
    }
}
